package com.vcredit.creditcat.manager.creditentity;

import java.util.List;
import java.util.Map;

import com.vcredit.creditcat.redis.RedisServiceExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.bird.util.BirdConstants;
import com.vcredit.creditcat.api.ICreditEntityManager;
import com.vcredit.creditcat.dto.creditentity.CreditEntityDto;
import com.vcredit.creditcat.model.CreditEntity;
import com.vcredit.creditcat.service.CreditEntityService;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.BeanUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class CreditEntityManager implements ICreditEntityManager {

	@Autowired
	private CreditEntityService creditEntityService;
	@Autowired
	private RedisServiceExt redisExtService;
	
	@SuppressWarnings("unchecked")
	@Log
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public List<CreditEntityDto> creditEntityQuery(Integer pageIndex,Integer showPosition) throws Exception {
		List<CreditEntityDto> CreditTipsList = null;
		if((showPosition!=null && showPosition.equals(2))||0 == pageIndex){
			List<CreditEntity> resultList = creditEntityService.findAll();
			if (resultList != null && resultList.size() > 0) {
				CreditTipsList = BeanUtil.copy(resultList, CreditEntityDto.class);
			}
		}else{
			Map<Integer,List<CreditEntityDto>> creditEntityMap =
					(Map<Integer,List<CreditEntityDto>>) redisExtService.get(BirdConstants.CREDIT_ENTITY_KEY);
			if(null != creditEntityMap && !creditEntityMap.isEmpty()){
				CreditTipsList = creditEntityMap.get(pageIndex);//得到每个key多对用value的值
			}
		}
		return CreditTipsList;
	}
}
